package com.ubi.core.framework.monitor.health;

import com.ubi.core.utils.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.StringRedisTemplate;

public class RedisHealthChecker implements HealthChecker {

    @Autowired
    @Lazy
    StringRedisTemplate stringRedisTemplate;

    @Override
    public Health check() {
        Health health = new Health();
        health.setName("redis");
        StopWatch stopWatch = new StopWatch();
        try {
            stringRedisTemplate.boundValueOps("monitor").get();
            health.setStatus(Health.STATUS_OK);
        } catch (Exception e) {
            health.setStatus(Health.STATUS_ERROR);
        }
        health.setElapsedTime(stopWatch.elapsedTime());
        return health;
    }

}
